/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Bean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import java.util.ArrayList;
import ProjectMgtSystem.Utilities.*;
import ProjectMgtSystem.Entities.User;
import java.sql.*;

/**
 *
 * @author Nguyen Phu Cuong
 * Created: 2011/07/13
 * Last Updated: Su Duy Hien 2011/07/18
 */
public class LoginForm extends org.apache.struts.validator.DynaValidatorForm {

    private String userName;
    private String password;
    private String usertype;

    /**
     *
     */
    public LoginForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

        ActionErrors errors = super.validate(mapping, request);
        return errors;
    }

    public User checkLogin(String name, String password) throws SQLException {
        User user = new User();
        int usertype = 0;
        String userid;
        DBHelper.getInstance().initConnection();
        ArrayList<String> param = new ArrayList<String>();
        param.add(name);
        param.add(password);
        ResultSet rs = DBHelper.getInstance().executeQueryProc("CheckLogin", param);
        if (rs != null) {
            while (rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                int numberOfColumns = rsmd.getColumnCount();
                if (numberOfColumns == 1) {
                    user.setMessage(rs.getString("message"));
                } else {
                    usertype = rs.getInt("usertype");
                    userid = rs.getString("userid");
                    user.setUserid(userid);
                    user.setUsertype(usertype);
                }
            }
            DBHelper.getInstance().closeConnection();
        }
        return user;
    }

    /**
     * @return the userName
     */
    public String getUsername() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUsername(String username) {
        this.userName = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
}
